<template>
  <!-- 租房 -->
  <div class="box">
    <heads></heads>
    <tools></tools>
    <div class="tit1">
      <div class="list">
        <div class="li" style="cursor: pointer;" @click="() => {
          $router.push('/');
        }
          ">
          <img src="../../assets/logo.png" />
          <!-- <span>帮帮同城</span> -->
        </div>
        <div :style="olds == 0 ? 'color:#64B6A8;' : ''" @click="cutHome(2, 2)">
          二手房
        </div>
        <div :style="olds == 1 ? 'color:#64B6A8;' : ''" @click="cutHome(2, 1)">
          新房
        </div>
        <div :style="olds == 2 ? 'color:#64B6A8;' : ''">租房</div>
        <div class="sousuo">
          <input type="text" v-model="searchtext" @keyup.enter="search" placeholder="请输入区域小区名称开始找房" />
          <p class="el-icon-search" @click="search"></p>
        </div>
        <!-- <div class="but">下载帮帮同城app</div> -->
      </div>
    </div>
    <div class="saixuan">
      <div class="li">
        <p class="p1">类别</p>
        <p class="p2" @click="leibieClick(-1)" :class="leibieIndex == -1 ? 'xuan' : ''">
          不限
        </p>
        <div class="list">
          <div v-for="(item, index) in type1" :key="index" @click="leibieClick(item)"
            :class="leibieIndex == item.valueNum ? 'xuan' : ''">
            {{ item.valueName }}
          </div>
        </div>
      </div>
      <div class="li">
        <p class="p1">区域</p>
        <p class="p2" @click="quyuClick(-1)" :class="quyuIndex == -1 ? 'xuan' : ''">
          不限
        </p>
        <div class="list">
          <div v-for="(item, index) in type7" :key="index" @click="quyuClick(item)"
            :class="quyuIndex == item.valueNum ? 'xuan' : ''">
            {{ item.valueName }}
          </div>
        </div>
      </div>
      <div class="li">
        <p class="p1">租金</p>
        <p class="p2" @click="priceClick(-1)" :class="priceIndex == -1 ? 'xuan' : ''">
          不限
        </p>
        <div class="list">
          <div v-for="(item, index) in type8" :key="index" @click="priceClick(item)"
            :class="priceIndex == item.valueNum ? 'xuan' : ''">
            {{ item.valueName }}
          </div>
        </div>
      </div>
      <div class="li">
        <p class="p1">面积</p>
        <p class="p2" @click="mianjiClick(-1)" :class="mianjiIndex == -1 ? 'xuan' : ''">
          不限
        </p>
        <div class="list">
          <div v-for="(item, index) in type9" :key="index" @click="mianjiClick(item)"
            :class="mianjiIndex == item.valueNum ? 'xuan' : ''">
            {{ item.valueName }}
          </div>
        </div>
      </div>
      <div class="li">
        <p class="p1">楼层</p>
        <p class="p2" @click="loucClick(-1)" :class="loucIndex == -1 ? 'xuan' : ''">
          不限
        </p>
        <div class="list">
          <div v-for="(item, index) in type10" :key="index" @click="loucClick(item)"
            :class="loucIndex == item.valueNum ? 'xuan' : ''">
            {{ item.valueName }}
          </div>
        </div>
      </div>
    </div>
    <div class="yixuan" v-if="screenList.length > 0">
      <div class="na">已选条件：</div>
      <div class="li" v-for="(item, index) in screenList" :key="item.type + item.id">
        {{ item.value }}
        <img src="../../assets/cha.png" @click="clear(item)" />
      </div>
      <div class="qinchu" @click="clear(-1)">
        清除全部
      </div>
    </div>
    <div class="bottom">
      <div class="titLiat">
        <div @click="titClick(item.num)" :class="xuanzhong == item.num ? 'li cur' : 'li'" v-for="(item, index) in list"
          :key="index">
          <p>{{ item.name }}</p>
          <img v-if="item.num == xuanzhong" src="../../assets/xuanzhong.png" />
        </div>
      </div>
      <div class="list">
        <div class="li" v-for="(item, index) in housesList" :key="index">
          <img class="biao" @click="details(item.id, $event)"
            style="cursor: pointer;object-fit: contain;background-color: #00000033;" :src="(item.houseImage && item.houseImage.split('|')[0]) ||
              (item.house_image && item.house_image.split('|')[0])
              " />
          <div class="duibi" @click="jiaru(item.id)" @mouseout="jiaruChu" v-if="jiaruXuan == item && !item.houseContrast">
            <p>加入对比</p>
          </div>
          <div class="duibi" @click="duibiqx(item.id)" @mouseout="jiaruChu"
            v-if="jiaruXuan == item && item.houseContrast">
            <p>取消对比</p>
          </div>
          <div class="name" @click="details(item.id, $event)">
            <div class="t_1" @mouseover="jiaruJin1(item)">
              <tag-view :item="item"></tag-view>{{ item.title }}
            </div>
            <img class="t_1_1" v-if="jiaruXuan1 == item && !item.houseContrast" @mouseover="jiaruJin1(item)"
              @click="jiaruChu1(item.id)" src="../../assets/duibi1.png" />
            <img class="t_1_1" v-if="jiaruXuan1 == item && item.houseContrast" @mouseover="jiaruJin1(item)"
              @click="duibiqx(item.id)" src="../../assets/qxduibi.png" />
            <div class="t_2">
              <span style="margin-right: 5px">{{ item.region }}</span>
              <span>{{ item.address }}</span>
            </div>
            <div class="t_3">
              <span style="margin-right: 16px">{{ item.house_type }}</span>
              <span style="margin-right: 16px">{{ item.AREA }}㎡</span>
              <span>{{ item.FLOOR }}</span>
            </div>
            <div class="t_4">
              <img style="border-radius: 31px;" :src="item.image ||
                'https://pic.bangbangtongcheng.com/static/my/header.png'
                " />
              <span style="cursor: pointer;" @click="toAgent(item)">{{
                item.nikeName || "未命名用户"
              }}</span>
              <div style="cursor: pointer;" @click="shopdetails(item)">
                {{ item.shop_name }}
              </div>
            </div>
          </div>
          <div class="qian">
            <p class="p1">{{ item.price }}元</p>
            <!-- <p class="p2">均价:{{(item.price / item.AREA).toFixed(2)}}元/㎡</p> -->
          </div>
        </div>
      </div>
      <div class="fenye">
        <el-pagination background layout="prev, pager, next" :total="housesTotal" :current-page="HousesPage.PageNo"
          @current-change="HousesCurrentChange" :page-size="HousesPage.pageSize"></el-pagination>
      </div>
    </div>
    <floor></floor>
    <div class="gotop" @click="toTop" v-show="showToTop" style="left: calc(100vw / 2 + 600px);;">
      <img src="@/assets/top.png" />返回顶部
    </div>
  </div>
</template>
<script>
import floor from "../../components/floor";
import heads from "../../components/head";
import tools from "../../components/tools";
export default {
  components: {
    floor,
    heads,
    tools
  },
  data() {
    return {
      olds: 2,
      xuanzhong: 0,
      list: [
        {
          num: 0,
          name: "全部"
        },
        {
          num: 2,
          name: "个人"
        },
        {
          num: 1,
          name: "经纪人"
        }
      ],
      jiaruXuan: "",
      jiaruXuan1: "",
      leibieIndex: -1,
      quyuIndex: -1,
      priceIndex: -1,
      loucIndex: -1,
      mianjiIndex: -1,
      HousesPage: {
        pageNo: 1,
        pageSize: 10,
        transactionType: 1,
        upStatus: 1,
        examineStatus: 2,
        isLz: "0",
        orderSort: "1"
      },
      housesList: [],
      housesTotal: 0,
      type7: [], //区域
      type8: [], //租金
      type9: [], //面积
      type10: [], //楼层
      type1: [], //类别
      searchtext: "",
      screenList: []
    };
  },
  mounted() { },
  created() {
    this.init();
  },
  methods: {
    checkFilterData(arr = [], id, type) {
      return arr.filter(item => item.ftype == type && item.id == id).length > 0;
    },
    clear(item) {
      if (item != -1) {
        var type = item.ftype;
        if (type == "type") {
          //类别
          this.leibieClick(-1);
        } else if (type == "region") {
          //区域
          this.quyuClick(-1);
        } else if (type == "price") {
          // 价格
          this.priceClick(-1);
        } else if (type == "area") {
          // 面积
          this.mianjiClick(-1);
        } else if (type == "floor") {
          // 楼层
          this.loucClick(-1);
        }
      } else {
        this.leibieIndex = -1;
        delete this.HousesPage.type;
        this.quyuIndex = -1;
        delete this.HousesPage.region;
        this.priceIndex = -1;
        delete this.HousesPage.price;
        this.mianjiIndex = -1;
        delete this.HousesPage.area;
        this.loucIndex = -1;
        delete this.HousesPage.floor;
        this.getHouseInfo();
      }
    },
    dealOPe(newV) {
      var screenList = [];
      var checkArr = [];
      var labelKey = "";
      var obj = {};
      for (const key in newV) {
        if (Object.hasOwnProperty.call(newV, key)) {
          const element = newV[key];
          if (
            key == "type" ||
            key == "price" ||
            key == "region" ||
            key == "area" ||
            key == "floor"
          ) {
            labelKey = "";
            if (key == "type") {
              //类别
              checkArr = this.type1;
              labelKey = "valueNum";
            } else if (key == "region") {
              //区域
              checkArr = this.type7;
            } else if (key == "price") {
              // 价格
              checkArr = this.type8;
            } else if (key == "area") {
              // 面积
              checkArr = this.type9;
            } else if (key == "floor") {
              // 楼层
              labelKey = "valueNum";
              checkArr = this.type10;
            }
            labelKey = labelKey || "valueName";
            console.log(element, checkArr);
            obj = checkArr
              .filter(item => item[labelKey] == element)
              .map(item => {
                return {
                  ...JSON.parse(JSON.stringify(item)),
                  value: item.valueName,
                  ftype: key
                };
              });
            if (!this.checkFilterData(this.screenList, obj.id, key)) {
              console.log("进来了进来了");
              screenList.push(...obj);
              console.log(screenList);
            }
          }
        }
      }
      this.screenList = screenList;
    },
    init() {
      this.getHouseInfo();
      this.getDictionaries();
    },
    //查询房源信息
    getHouseInfo() {
      this.dealOPe(JSON.parse(JSON.stringify(this.HousesPage)));
      this.$getHttp("/mob/home/getHouseInfo", this.HousesPage).then(data => {
        if (data.code == 200) {
          //二手房 房源较少时出现推荐房源
          // this.recomstate = false
          // if(this.HousesPage.newOld == 2 && data.count < 3){
          // 	this.recomstate = true
          // }
          const contrastIdList = JSON.parse(
            localStorage.getItem("contrastIdList")
          );
          //处理楼层信息
          data.list.forEach(e => {
            if (contrastIdList != null) {
              e.houseContrast = false;
              contrastIdList.forEach(el => {
                if (el == e.id) {
                  e.houseContrast = true;
                }
              });
            }
            if (e.hasOwnProperty("FLOOR")) {
              if (e.FLOOR.indexOf("|") == -1) {
                e.FLOOR = "";
              } else {
                e.FLOOR =
                  e.FLOOR.split("|")[0] +
                  "层/" +
                  "共" +
                  e.FLOOR.split("|")[1] +
                  "层";
              }
            }
            if (!e.hasOwnProperty("shop_name")) {
              e.shop_name = "个人房源";
            }
          });
          console.log("object", data.list);
          this.housesList = data.list;
          this.housesTotal = data.count;
        }
      });
    },
    //查询字典(筛选条件)
    getDictionaries() {
      this.$getHttp("/mob/home/getDictionaries", { type: "7,12,9,10,1" }).then(
        data => {
          if (data.code == 200) {
            data.list.forEach(e => {
              if (e.type == 7) this.type7.push(e);
              if (e.type == 12) this.type8.push(e);
              if (e.type == 9) this.type9.push(e);
              if (e.type == 10) this.type10.push(e);
              if (e.type == 1) this.type1.push(e);
            });
          }
        }
      );
    },
    search() {
      if (this.searchtext.trim() != "") {
        const routeLink = this.$router.resolve({
          path: "/index/houseSearch",
          query: {
            search: this.searchtext,
            transactionType: 1
          }
        });
        window.open(routeLink.href, "_blank");
      }
      // this.openUrl({
      //   path: "/index/houseSearch",
      //   query: {
      //     search: this.searchtext,
      //   },
      // });
    },
    floor(a) {
      var fool = a.split(",");
      if (fool[0] == 1) return fool[1] + "层/" + fool[2] + "层";
      if (fool[0] == 2)
        return fool[1] + "层-" + fool[2] + "层/共" + fool[3] + "层";
      if (fool[0] == 3) return "独栋/共" + fool[1] + "层";
    },
    cutHome(type, old) {
      this.openUrl({
        path: "/index/newHouse",
        query: {
          transactionType: type,
          newOld: old
        }
      });
    },
    titClick(index) {
      this.xuanzhong = index;
      this.HousesPage.pageNo = 1;
      if (index != 0) {
        this.HousesPage.userType = index;
      } else {
        delete this.HousesPage.userType;
      }
      this.getHouseInfo();
    },
    leibieClick(item) {
      this.HousesPage.pageNo = 1;
      if (item != -1) {
        this.leibieIndex = item.valueNum;
        this.HousesPage.type = item.valueNum;
        this.getHouseInfo();
        return;
      }
      this.leibieIndex = item;
      delete this.HousesPage.type;
      this.getHouseInfo();
    },
    quyuClick(item) {
      this.HousesPage.pageNo = 1;
      if (item != -1) {
        this.quyuIndex = item.valueNum;
        this.HousesPage.region = item.valueName;

        this.getHouseInfo();
        return;
      }
      this.quyuIndex = item;
      delete this.HousesPage.region;
      this.getHouseInfo();
    },
    priceClick(item) {
      this.HousesPage.pageNo = 1;
      if (item != -1) {
        this.priceIndex = item.valueNum;
        this.HousesPage.price = item.valueName;
        this.getHouseInfo();
        return;
      }
      this.priceIndex = item;
      delete this.HousesPage.price;
      this.getHouseInfo();
    },
    mianjiClick(item) {
      this.HousesPage.pageNo = 1;
      if (item != -1) {
        this.mianjiIndex = item.valueNum;
        this.HousesPage.area = item.valueName;
        this.getHouseInfo();
        return;
      }
      this.mianjiIndex = item;
      delete this.HousesPage.area;
      this.getHouseInfo();
    },
    loucClick(item) {
      this.HousesPage.pageNo = 1;
      if (item != -1) {
        this.loucIndex = item.valueNum;
        this.HousesPage.floor = item.remarks;

        this.getHouseInfo();
        return;
      }
      this.loucIndex = item;
      delete this.HousesPage.floor;
      this.getHouseInfo();
    },
    HousesCurrentChange(e) {
      this.scrollToElement(document.querySelector(".bottom"));
      this.HousesPage.pageNo = e;
      this.housesList = [];
      this.getHouseInfo();
    },

    jiaru(id) {
      this.jiaruduibi(id);
    },
    jiaruJin(num) {
      this.jiaruXuan = num;
    },
    jiaruChu() {
      this.jiaruXuan = "";
    },
    jiaruJin1(num) {
      this.jiaruXuan1 = num;
    },
    jiaruChu1(id) {
      this.jiaruXuan1 = "";
      this.jiaruduibi(id);
      window.event ? (window.event.cancelBubble = true) : e.stopPropagation(); //阻止冒泡
    },
    duibiqx(id) {
      this.quxiaoduibi(id);
    },
    jiaruduibi(id) {
      let contrastIdList = JSON.parse(localStorage.getItem("contrastIdList"));
      if (contrastIdList && contrastIdList.length >= 4) {
        this.$message({
          message: "最多4套进行对比",
          type: "error",
          offset: 120,
          duration: 2000
        });
        return;
      }
      if (contrastIdList == null) {
        let list = [id];
        localStorage.setItem("contrastIdList", JSON.stringify(list));
      } else {
        contrastIdList.push(id);
        localStorage.setItem("contrastIdList", JSON.stringify(contrastIdList));
      }
      this.housesList.forEach(e => {
        if (e.id == id) {
          e.houseContrast = true;
        }
      });
      this.$message({
        message: "已加入对比！",
        type: "success",
        offset: 120,
        duration: 2000
      });
    },
    quxiaoduibi(id) {
      let contrastIdList = JSON.parse(localStorage.getItem("contrastIdList"));
      for (var i = 0; i < contrastIdList.length; i++) {
        if (contrastIdList[i] == id) {
          contrastIdList.splice(i, 1);
        }
      }
      localStorage.setItem("contrastIdList", JSON.stringify(contrastIdList));
      this.housesList.forEach(e => {
        if (e.id == id) {
          e.houseContrast = false;
        }
      });
      this.$message({
        message: "已取消对比！",
        type: "error",
        offset: 120,
        duration: 2000
      });
    },
    details(id, event) {
      if (event.target._prevClass == "t_1_1") {
        console.log("1111111111111111111111");
      } else {
        this.openUrl({
          path: "/index/leaseDetails",
          query: {
            id: id
          }
        });
      }
    },
    shopdetails(item) {
      if (item.shop_name == "个人房源") {
        this.$message({
          message: "个人房源！",
          type: "error",
          offset: 120,
          duration: 2000
        });
      } else {
        this.openUrl({
          path: "/index/ShopDetails",
          query: {
            id: item.shop_id
          }
        });
      }
    },
    toAgent(item) {
      // if (!item.shop_id) {
      //   this.openUrl({
      //     path: "/index/userDetail",
      //     query: {
      //       id: item.user_id
      //     }
      //   });
      //   return;
      // }
      this.openUrl({
        path: "/index/goldConsultant",
        query: {
          id: item.user_id
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.box {
  position: relative;
  padding-bottom: 200px;
}

.tit1 {
  width: 100%;
  height: 120px;
  background-color: #f2f2f2;

  .list {
    width: 1200px;
    margin-left: 50%;
    padding-top: 32px;
    transform: translateX(-50%);
    background-color: #f2f2f2;

    div {
      display: inline-block;
      vertical-align: middle;
      font-size: 20px;
      color: #333333;
      margin-right: 40px;
      cursor: pointer;
    }

    .li {
      font-size: 28px;
      color: #64b6a8;
      margin-right: 60px;

      img {
        /* width: 60px;
        height: 60px; */
      }

      span {
        vertical-align: middle;
      }
    }

    .sousuo {
      position: relative;
      display: inline-block;
      vertical-align: middle;
      width: 380px;
      height: 50px;
      background: #ffffff;
      border-radius: 4px;
      font-size: 18px;
      overflow: hidden;

      input {
        width: 300px;
        height: 55px;
        line-height: 55px;
        text-indent: 16px;
        font-size: 18px;
      }

      img {
        width: 25px;
        height: 25px;
        margin-left: 20px;
        cursor: pointer;
      }

      p {
        position: absolute;
        top: 0;
        right: 0;
        width: 80px;
        height: 55px;
        background-color: #64b6a8;
        text-align: center;
        line-height: 55px;
        font-size: 24px;
        cursor: pointer;
        color: #ffffff;
      }
    }

    .but {
      display: inline-block;
      width: 200px;
      height: 50px;
      background: #64b6a8;
      border-radius: 6px;
      text-align: center;
      line-height: 50px;
      color: #fff;
    }
  }
}

.saixuan {
  color: #333;
  width: 1200px;
  margin-left: 50%;
  transform: translateX(-50%);
  font-size: 0;
  margin-top: 40px;
  padding-bottom: 20px;

  .li {
    .p1 {
      display: inline-block;
      font-size: 16px;
      font-weight: 600;
      margin-right: 32px;
      vertical-align: top;
    }

    .p2 {
      display: inline-block;
      font-size: 16px;
      margin-right: 32px;
      vertical-align: top;
      cursor: pointer;
    }

    div {
      display: inline-block;
      font-size: 16px;
      margin-right: 30px;
      vertical-align: top;
      margin-bottom: 10px;
    }

    .list {
      width: 1040px;

      div {
        cursor: pointer;
      }
    }

    .xuan {
      color: #64b6a8;
    }
  }
}

.bottom {
  width: 1200px;
  margin-left: 50%;
  transform: translateX(-50%);
  color: #333333;
  margin-top: 0px;

  .tit {
    font-weight: 600;
    font-size: 32px;
    margin-bottom: 24px;
  }

  .list {
    width: 100%;

    .li {
      position: relative;
      padding-bottom: 24px;
      margin-bottom: 24px;
      border-bottom: 1px solid #e0e0e0;

      .biao {
        width: 300px;
        height: 200px;
        cursor: pointer;
        border-radius: 10px;
      }

      .duibi {
        position: absolute;
        width: 300px;
        height: 200px;
        top: 0;
        left: 0;
        background-color: rgba(51, 51, 51, 0.64);
        cursor: pointer;

        p {
          position: absolute;
          width: 130px;
          height: 48px;
          line-height: 48px;
          text-align: center;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          border-radius: 24px;
          color: #fff;
          border: 2px solid #ffffff;
          font-size: 21px;
        }
      }

      .name {
        display: inline-block;
        vertical-align: top;
        margin-left: 24px;
        position: relative;
        width: 700px;
        height: 200px;

        .t_1 {
          position: relative;
          font-size: 26px;
          cursor: pointer;
          padding-bottom: 12px;

          .t_1_1 {
            position: absolute;
            width: 130px;
            height: 58px;
            top: 30px;

            //right: -90px;
            img {
              width: 100%;
            }
          }
        }

        .t_1_1 {
          position: absolute;
          width: 130px;
          height: 58px;
          top: 30px;
          left: 160px;

          //right: -90px;
          img {
            width: 100%;
          }
        }

        .t_2 {
          font-size: 18px;
          color: #666666;
        }

        .t_3 {
          font-size: 18px;
          color: #666666;
          margin-top: 12px;
        }

        .t_4 {
          position: absolute;
          font-size: 18px;
          color: #666666;
          bottom: 0;
          left: 0;

          img {
            width: 31px;
            height: 31px;
          }

          div {
            display: inline-block;
            //width: 90px;
            padding: 0px 5px;
            height: 30px;
            background: #ffffff;
            border: 1px solid #64b6a8;
            border-radius: 4px;
            text-align: center;
            line-height: 30px;
            margin-left: 24px;
            color: #64b6a8;
          }
        }
      }

      .qian {
        position: absolute;
        top: 62px;
        right: 0;
        width: 180px;

        .p1 {
          width: 100%;
          font-size: 32px;
          color: #e00000;
          text-align: center;
          margin-bottom: 15px;
        }

        .p2 {
          width: 100%;
          font-size: 18px;
          color: #333333;
          text-align: center;
        }
      }
    }
  }

  .titLiat {
    width: 1200px;
    margin-left: 50%;
    transform: translateX(-50%);
    margin-bottom: 16px;

    .li {
      display: inline-block;
      vertical-align: top;
      margin-right: 40px;
      color: #333;
      cursor: pointer;

      p {
        font-size: 24px;
      }

      img {
        width: 40px;
        height: 20px;
        margin-left: 50%;
        transform: translateX(-50%);
      }
    }

    .cur {
      color: #64b6a8;
      border: 0;
      transform: scale(1.1);
    }
  }
}

.fenye {
  width: 100%;
  height: 50px;
  text-align: center;
  margin: 60px 0px;

  /deep/.el-pagination.is-background .el-pager li:not(.disabled).active {
    background-color: #64b6a8;
  }
}

.yixuan {
  box-sizing: border-box;
  padding-top: 15px;
  padding-left: 16px;
  width: 1200px;
  min-height: 60px;
  background-color: #f7f7f7;
  margin-left: 50%;
  transform: translateX(-50%);
  font-size: 16px;

  .na {
    color: #333333;
    display: inline-block;
    vertical-align: middle;
    margin-right: 6px;
  }

  .li {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    padding: 4px 28px 4px 10px;
    color: #333333;
    color: #64b6a8;
    border: 1px solid #64b6a8;
    margin-right: 12px;

    img {
      position: absolute;
      width: 17px;
      height: 17px;
      top: 7px;
      right: 3px;
      cursor: pointer;
    }
  }

  .qinchu {
    display: inline-block;
    vertical-align: middle;
    width: 86px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    background-color: #999999;
    font-size: 16px;
    color: #ffffff;
    cursor: pointer;
  }

  .qinchu,
  .li,
  .na {
    margin-bottom: 12px;
  }
}
</style>
